package reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test10 {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        test01();
        test02();
        test03();

    }


    public static void test01(){
        long startTime = System.currentTimeMillis();
        User user = new User();
        for (int i = 0; i < 100000000; i++) {
            user.getName();
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }

    public static void  test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        long startTime = System.currentTimeMillis();
        User user = new User();
        Class<? extends User> aClass = user.getClass();
        Method getName = aClass.getDeclaredMethod("getName", null);

        for (int i = 0; i < 100000000; i++) {
            getName.invoke(user,null);

        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }

    public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        long startTime = System.currentTimeMillis();
        User user = new User();
        Class<? extends User> aClass = user.getClass();
        Method getName = aClass.getDeclaredMethod("getName", null);
        getName.setAccessible(true);
        for (int i = 0; i < 100000000; i++) {
            getName.invoke(user,null);

        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }
}
